/*=====================================================================================================================
                    Copyright(c) 2009 Geometric PLM Software Corp. All rights reserved.
                             Unpublished - All rights reserved
=======================================================================================================================
File description:
    Filename: tc_sa.h
    Module  : (geometric) itk module.

        This file manages the operations of system administrator.

=======================================================================================================================
Date               Name              Description of Change
29-Jul-2009        Penn,Xie          Initialize creation
$HISTORY$
=====================================================================================================================*/
#include <sample_err.h>
#include <group.h>

#include "vector_c.h"
#include "tc_sa.h"

// For disable C4312
#ifdef WIN32
#pragma warning( push )
#pragma warning( disable: 4311 )
#pragma warning( disable: 4312 )
#endif

int GSA_get_dba_users( int *count, tag_t **users )
{
    int   ifail         = ITK_ok;

    int   i             = 0;
    Vector_t *userList  = VECTOR_new( 30, 10 );

    *count = 0;
    *users = NULL;

    ifail = GSA_get_dba_vector( &userList );
    if (ifail == ITK_ok)
    {
        *count  = VECTOR_size( userList );
        if ( *count != 0)
        {
            *users  = (tag_t*)MEM_alloc( *count * sizeof(tag_t) );
            for ( i = 0; i < *count; i++ )
            {
                (*users)[i] = (tag_t)VECTOR_get( userList, i );
            }
        }

        VECTOR_free( userList );
    }

    return ifail;
}

int GSA_get_dba_vector( Vector_t **dbaList )
{
    int   ifail         = ITK_ok;

    int     i             = 0;
    int     j             = 0;
    int     groupNum      = 0;
    tag_t   *groups       = NULL;
    int     privlilege    = GSA_NOT_DBA_PRIVILEGE;
    int     memberNum     = 0;
    tag_t   *members      = NULL;
    logical isDeactived   = false;
    tag_t   user          = NULLTAG;


    ifail = SA_extent_group( &groupNum, &groups );
    CALL( ifail );

    for (i = 0; (i < groupNum && ifail == ITK_ok); i++ )
    {
        ifail = SA_ask_group_system_priv( groups[i], &privlilege );
        if (ifail != ITK_ok)
            break;
        if (privlilege != GSA_DBA_PRIVILEGE)
            continue;

        // Traversal
        ifail = SA_find_groupmembers_by_group ( groups[i], &memberNum, &members );
        if (ifail != ITK_ok)
            break;

        for (j = 0; (j < memberNum && ifail == ITK_ok); j++)
        {
            ifail = SA_ask_groupmember_inactive( members[j], &isDeactived );
            if (ifail == ITK_ok && isDeactived == false)
            {
                ifail = SA_ask_groupmember_user( members[j], &user );
                if (ifail == ITK_ok)
                {
                    *dbaList = VECTOR_add( *dbaList, (void*)user );
                }
            }
        }
        MEM_free( members );
    }

    MEM_free( groups );

    return ifail;
}

// For recover warning
#ifdef WIN32
#pragma warning( pop )
#endif

